/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.idea.utils.sfo.designer;


import designersfo.Cons;
import java.beans.IntrospectionException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.TreeMap;
import org.openide.nodes.Index;
import org.openide.nodes.Node;
import org.openide.util.Exceptions;


/**
 * This class provides the necessary functionality to display a list of children
 * nodes in an explorer window.
 *
 * These nodes represent the cells contained in a <gids-designer:cons> section
 * of a designer SFO file.
 *
 * @author renke002
 */
public class DesignerColChildren extends Index.ArrayChildren
{

    private ArrayList<Node> childrenNodes;


    /**
     *
     * @param row
     */
    DesignerColChildren( DesignerRow row )
    {
        buildChildrenNodesList( row );
    }


    /**
     * Builds the list of children nodes.
     *
     * The following code used to be located in initCollection() and would be
     * run when needed however, in order to display the elements on the canvas,
     * we need the list of children nodes immediately. So the list of children
     * nodes is now a global object in this class and it gets created here.
     *
     * @param row - DesignerRow
     */
    private void buildChildrenNodesList( DesignerRow row )
    {
        this.childrenNodes = new ArrayList<Node>();

        //
        TreeMap< Integer , Cons> colMap = row.getColMap();

        //
        TreeMap< Integer , Long> colWidths = row.getColWidths();

        long prevColWidths = 0;
        /* loop through all the children, column by column
         */
        for ( Integer i : colMap.keySet() )
        {
            /**
             * @ SFO cons object
             * @ colNum
             * @ rowNum
             * @ myHeight
             * @ prevHeights
             * @ myWidth
             * @ prevColWidths
             */
            DesignerCol dc = new DesignerCol(
                    colMap.get( i ) ,
                    i ,
                    row.getRowNumber() ,
                    row.getRowHeight() ,
                    row.getPrevRowHeights() ,
                    colWidths.get( i ) ,
                    prevColWidths );

            // 
            prevColWidths += colWidths.get( i );

            // 
            try
            {
                childrenNodes.add( new DesignerColNode( dc ) );
            } catch ( IntrospectionException ex )
            {
                Exceptions.printStackTrace( ex );
            }
        }
    }


    /**
     *
     * @return
     */
    public HashSet<DesignerColNode> getDesignerColNodes()
    {
        HashSet<DesignerColNode> dcnSet = new HashSet<DesignerColNode>();

        for ( Node dcn : this.childrenNodes )
        {
            dcnSet.add( (DesignerColNode) dcn );
        }

        return dcnSet;
    }


    /**
     *
     * @return
     */
    @Override
    protected List<Node> initCollection()
    {
        return this.childrenNodes;
    }

}
